/**
 * Definition for singly-linked list.
 */
import java.util.Arrays;

  class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode cur = head;
        int count = 1;
        while(cur.next != null){
            cur = cur.next;
            count ++;
        }
        cur = head;
        int[] listVal = new int[count];
        for(int i = 0;i < count; i ++){
            listVal[i] = cur.val;
            cur = cur.next;
        }
        Arrays.sort(listVal);
        cur = head;
        for(int i = 0;i < count;i ++){
            cur.val = listVal[i];
            cur = cur.next;
        }
        return head;
    }
}